package com.wang.jmonkey.modules.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.jmonkey.modules.system.mapper.SystemButtonMapper;
import com.wang.jmonkey.modules.system.model.dto.SystemButtonDto;
import com.wang.jmonkey.modules.system.model.entity.SystemButtonEntity;
import com.wang.jmonkey.modules.system.model.param.SystemButtonParam;
import com.wang.jmonkey.modules.system.service.SystemButtonService;
import com.wang.jmonkey.modules.system.service.SystemResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 按钮及访问权限配置信息表(SystemButton)表服务实现类
 *
 * @author HeJiawang
 * @since 2021-05-21 21:44:20
 */
@Service
public class SystemButtonServiceImpl extends ServiceImpl<SystemButtonMapper, SystemButtonEntity> implements SystemButtonService {

    /**
     * mapper
     */
    @Autowired
    private SystemButtonMapper mapper;

    @Autowired
    private SystemResourceService systemResourceService;


    /**
     * 新增按钮及访问权限
     * @param button 按钮及访问权限信息
     * @return true 新增成功
     */
    @Override
    public boolean save(SystemButtonParam button) {
        SystemButtonEntity buttonEntity = button.convertToEntity();
        super.save(buttonEntity);

        return systemResourceService.save(
                button.convertToResource().setResourceId(buttonEntity.getId())
        );
    }

    /**
     * 修改按钮及访问权限
     * @param button 按钮及访问权限信息
     * @return true 修改成功
     */
    @Override
    public boolean updateById(SystemButtonParam button) {
        SystemButtonEntity buttonEntity = button.convertToEntity();

        return super.updateById(buttonEntity)
                && systemResourceService.updateParentId(button.convertToResource());
    }

    /**
     * 获取按钮权限数据
     * @param parentId 父级id，指向system_resource.parent_id
     * @return 按钮权限数据
     */
    @Override
    public List<SystemButtonDto> list(Long parentId) {
        return mapper.list(parentId);
    }
}
